<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>编辑任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/frontend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/frontend/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" autocomplete="off">
    <input type="hidden" name="id" value="{$task['id']}">
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" value="{$task['name']}" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">优先级</label>
        <div class="layui-input-block">
            <select name="priority" lay-verify="required" lay-search>
                <option value="低" {$task['priority'] == '低' ? 'selected' : ''}>低</option>
                <option value="中" {$task['priority'] == '中' ? 'selected' : ''}>中</option>
                <option value="高" {$task['priority'] == '高' ? 'selected' : ''}>高</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required" lay-search>
                <option value="需求任务" {$task['type'] == '需求任务' ? 'selected' : ''}>需求任务</option>
                <option value="缺陷任务" {$task['type'] == '缺陷任务' ? 'selected' : ''}>缺陷任务</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">时间计算管理</label>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 50px;">
                <select name="symbol" lay-verify="required" lay-search>
                    <option value="+" {$task['symbol'] == '+' ? 'selected' : ''}>+</option>
                    <option value="-" {$task['symbol'] == '-' ? 'selected' : ''}>-</option>
                </select>
            </div>
            <div class="layui-input-inline" style="width: 80px;">
                <input type="number" value="{$task['hour']}" id="hour" name="hour" placeholder="小时" autocomplete="off" class="layui-input" oninput="checkHour(this.value)">
            </div>
            <div class="layui-input-inline" style="width: 80px;">
                <input type="number" value="{$task['minute']}" id="minute" name="minute" placeholder="分钟" autocomplete="off" class="layui-input" oninput="checkMinute(this.value)">
            </div>
            <div class="layui-input-inline">
                <button type="button" id="timeSlice" class="layui-btn">时间片</button>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">详情描述</label>
        <div id="content" class="layui-input-block">
            {:htmlspecialchars_decode($task['content'])}
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script src="/static/frontend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/frontend/js/wangEditor.min.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 时间片管理
        $('#timeSlice').on('click', function () {
            var index = layer.open({
                title: '时间片',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: false,
                area: ['100%', '100%'],
                content: "/frontend/task/time-slice/index?id={$task['id']}",
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = {
                'id': data.field.id,
                'name': data.field.name,
                'priority': data.field.priority,
                'type': data.field.type,
                'content': editor.txt.html(),
                'symbol': data.field.symbol,
                'hour': data.field.hour,
                'minute': data.field.minute
            };
            $.post('/frontend/task/sub-task/update', postData, function (res) {
                if (res.code == 0) {
                    // 修改成功
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000
                    },function () {
                        // 重新加载父页面
                        parent.location.reload();
                    })
                } else if (res.code == 302) {
                    // 错误跳转
                    location.href = '/frontend/error/' + res.msg;
                } else {
                    // 修改失败
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });

        // 自定义验证规则
        form.verify({
            name: function (value, item) {
                if (value.length > 20) {
                    return '标题不能超过20个字符';
                }
            }
        })

    });

    function checkHour(value) {
        let re = /^[0-9]+$/;
        let result = re.test(value);
        if (result && value >= 0) {
            document.getElementById('hour').value = value;
        } else {
            document.getElementById('hour').value = 0;
        }
    }

    function checkMinute(value) {
        let re = /^[0-9]+$/;
        let result = re.test(value);
        if (result && value >= 0) {
            document.getElementById('minute').value = value;
        } else {
            document.getElementById('minute').value = 0;
        }
    }

    // 富文本编辑器
    const E = window.wangEditor;
    const editor = new E('#content');
    // 配置z-index
    editor.config.zIndex = 500;
    // 配置server接口地址
    editor.config.uploadImgServer = '/frontend/task/upload-img';
    editor.create();
</script>
</body>
</html>